package com.wish.renting.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.wish.base.entity.CwPaymentPlan;
import com.wish.base.entity.SfContract;
import com.wish.base.service.ISfContractService;
import com.wish.common.core.web.controller.BaseController;
import com.wish.common.core.web.page.TableDataInfo;
import com.wish.renting.config.AjaxResult;
import com.wish.renting.config.PDFUtils;
import com.wish.renting.service.PaymentPlanService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/*

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)

*/
@RestController
@RequestMapping("/contract")
public class TSfContractController extends BaseController {

    @Autowired
    private ISfContractService iSfContractService;

    @Autowired
    private PaymentPlanService paymentPlanService;

    /**
     * 列表查询
     */
    @GetMapping("/list")
    public TableDataInfo list(SfContract tSfContract) {
        QueryWrapper<SfContract> queryWrapper = new QueryWrapper();
        startPage();
        //查询合同编号
        if(!StringUtils.isEmpty(tSfContract.getSfContractNo())){
            queryWrapper.like("sf_contract_no",tSfContract.getSfContractNo());
        }
        //查询小区名称
        if(!StringUtils.isEmpty(tSfContract.getPremiseName())){
            queryWrapper.like("premise_name",tSfContract.getPremiseName());
        }
        //查询业主姓名
        if(!StringUtils.isEmpty(tSfContract.getOwnerName())){
            queryWrapper.like("owner_name",tSfContract.getOwnerName());
        }
        //查询录入日期
        if(tSfContract.getConInfoTime()!=null){
            queryWrapper.gt("con_info_time",tSfContract.getConInfoTime());
        }
        List<SfContract> list = iSfContractService.list(queryWrapper);
        return getDataTable(list);
    }


    @PostMapping("/getPaymentPlanList")
    public AjaxResult getPaymentPlanList(@RequestBody SfContract sfContract) {
        List<CwPaymentPlan> paymentPlans = paymentPlanService.getPaymentPlans(sfContract);
        return AjaxResult.ok(paymentPlans);
    }

    @PostMapping("addContract")
    public AjaxResult add(@RequestBody SfContract sfContract)  {
        Calendar now = Calendar.getInstance();
        System.out.println("年: " + now.get(Calendar.YEAR));
        System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");
        System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));
        System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));
        System.out.println("分: " + now.get(Calendar.MINUTE));
        System.out.println("秒: " + now.get(Calendar.SECOND));
        System.out.println("当前时间毫秒数：" + now.getTimeInMillis());
        System.out.println(now.getTime());
        Date d = new Date();
        System.out.println(d);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateNowStr = sdf.format(d);
        System.out.println("格式化后的日期：" + dateNowStr);
        sfContract.setSfContractNo("SFHT"+dateNowStr);
        iSfContractService.saveOrUpdate(sfContract);
        return AjaxResult.ok(sfContract);
    }

    @RequestMapping("pdfById")
    @ResponseBody
    public String pdfById(Long id) {
        System.err.println(id);
        String pdfName = UUID.randomUUID().toString() + ".pdf";
        //根据id查询
        SfContract sfContract = iSfContractService.getById(id);
        try {
            //创建原始的pdf文件
            PDFUtils.tableAndImg2pdf("D:/pic/"+pdfName, null
                    , null, "PDF导出", Arrays.asList("编号", "名称"), new PDFUtils.PDFWriter() {
                        @Override
                        public void execute(PdfPTable pdfPTable, Font font) throws BadElementException, IOException {
                            //将数据库的数据导入到pdf
                            pdfPTable.addCell(new PdfPCell(new Paragraph(sfContract.getSfContractNo().toString(), font)));
                            pdfPTable.addCell(new PdfPCell(new Paragraph(sfContract.getAccountName().toString(),font)));
                        }
                    });
            PDFUtils.markImage("d:/pic/20407103948.jpg", new FileInputStream("D:/pic/"+pdfName), new FileOutputStream("d:/pic/1" + pdfName), "111");
            return "1"+pdfName;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }



/**
    //导出【请填写功能名称】列表
    @PreAuthorize(hasPermi = "system:contract:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SfContract tSfContract) throws IOException
    {
        List<SfContract> list = SfContract.selectTSfContractList(tSfContract);
        ExcelUtil<SfContract> util = new ExcelUtil<SfContract>(SfContract.class);
        util.exportExcel(response, list, "contract");
    }


    //获取【请填写功能名称】详细信息
    @PreAuthorize(hasPermi = "system:contract:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tSfContractService.selectTSfContractById(id));
    }


    //新增【请填写功能名称】

    @PreAuthorize(hasPermi = "system:contract:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TSfContract tSfContract)
    {
        return toAjax(tSfContractService.insertTSfContract(tSfContract));
    }

    //修改【请填写功能名称】
    @PreAuthorize(hasPermi = "system:contract:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TSfContract tSfContract)
    {
        return toAjax(tSfContractService.updateTSfContract(tSfContract));
    }

    //删除【请填写功能名称】
    @PreAuthorize(hasPermi = "system:contract:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tSfContractService.deleteTSfContractByIds(ids));
    }
    */

}

